// pages/enroll/enroll.js
var app = getApp()
var host = app.globalData.host
var self = null

Page({
  data: {
    info: null,
    itemId: '',
    productId: '',
    groupId: '',
    
    groupArray: ["男子2岁A组", "男子2岁B组", "女子2岁G组", "男子3岁A组", "男子3岁B组", "女子3岁G组", "男子4岁A组", "男子4岁B组", "女子4岁G组", "男子5岁组", "女子5岁组", "Open组"],
    group: '',
    realName: '',
    idNo: '',
    group2: '',
    realName2: '',
    idNo2: '',
    contact: '',
    others: '',

    themeColor: '',
    defaultColor: '',
    borderColor: ''
  },
  onLoad: function (options) {
    self = this;
    self.setData({
      itemId: options.itemId ? options.itemId : '',
      productId: options.productId ? options.productId : '',
      groupId: options.groupId ? options.groupId : ''
    })
    self.getStyle();
    self.getInfo();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor
    })
  },
  getInfo() {
    var url = null;
    var params = null;
    if (self.data.itemId) {
      url = host + '/yq/item/info'
      params = {
        itemId: self.data.itemId
      }
    } else if (self.data.productId) {
      url = host + '/yq/product/info'
      params = {
        productId: self.data.productId
      }
    }
    var success = function (res) {
      console.log(res);
      self.setData({
        info: res.info
      })
    };
    var fail = function (res) { };
    var complete = function () { };
    app.request(url, params, success, fail, complete);
  },
  pickerChange(e) {
    self.setData({
      group: self.data.groupArray[e.detail.value]
    })
  },
  realNameChange(e) {
    self.setData({
      realName: e.detail.value
    })
  },
  idNoChange(e) {
    self.setData({
      idNo: e.detail.value
    })
  },
  pickerChange2(e) {
    self.setData({
      group2: self.data.groupArray[e.detail.value]
    })
  },
  realNameChange2(e) {
    self.setData({
      realName2: e.detail.value
    })
  },
  idNoChange2(e) {
    self.setData({
      idNo2: e.detail.value
    })
  },
  contactChange(e) {
    self.setData({
      contact: e.detail.value
    })
  },
  othersChange(e) {
    self.setData({
      others: e.detail.value
    })
  },
  getPhoneNumber(e) {
    console.log(e);
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      wx.checkSession({
        success() {
          var url = host + '/yq/auth/getPhoneNumber';
          var params = {
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv
          };
          var success = function (res) {
            self.setData({
              contact: res.info
            })
          }
          var fail = function (res) {
          };
          var complete = function () { };
          app.request(url, params, success, fail, complete);
        },
        fail() {
          // app.loginWechat(this.getPhoneNumber);
        }
      })

    } else {
      wx.showToast({
        title: '获取手机号失败',
        icon: 'none'
      })
    }
  },
  onSubmit() {
    if (!wx.getStorageSync("openId")) {
      wx.showModal({
        title: '提示',
        content: '还没允许获取用户信息',
        confirmText: '去设置',
        success: function (res) {
          if (res.confirm) {
            wx.openSetting({
              success: (res) => {
              }
            })
          }
        }
      })
      return;
    }
    if (!self.data.group) {
      wx.showToast({
        title: '请选择组别',
        icon: 'none'
      })
      return;
    }
    if (!self.data.realName) {
      wx.showToast({
        title: '请填写真实姓名',
        icon: 'none'
      })
      return;
    }
    if (!self.data.idNo) {
      wx.showToast({
        title: '请填写身份证号',
        icon: 'none'
      })
      return;
    }
    if (self.data.idNo.length != 7 && self.data.idNo.length != 8 && self.data.idNo.length != 18) {
      wx.showToast({
        title: '请填写正确的身份证号',
        icon: 'none'
      })
      return;
    }
    if (!self.data.contact) {
      wx.showToast({
        title: '请填写联系电话',
        icon: 'none'
      })
      return;
    }
    var remark = "组别：" + self.data.group + "，真实姓名：" + self.data.realName + "，身份证号：" + self.data.idNo + "，联系电话：" + self.data.contact;
    
    if (self.data.productId == 20 || self.data.productId == 22 || self.data.productId == 23) {
      if (!self.data.group2) {
        wx.showToast({
          title: '请选择组别',
          icon: 'none'
        })
        return;
      }
      if (!self.data.realName2) {
        wx.showToast({
          title: '请填写真实姓名',
          icon: 'none'
        })
        return;
      }
      if (!self.data.idNo2) {
        wx.showToast({
          title: '请填写身份证号',
          icon: 'none'
        })
        return;
      }
      if (self.data.idNo2.length != 7 && self.data.idNo2.length != 8 && self.data.idNo2.length != 18) {
        wx.showToast({
          title: '请填写正确的身份证号',
          icon: 'none'
        })
        return;
      }
      remark = "参赛者一姓名：" + self.data.realName + "，参赛者一身份证：" + self.data.idNo + "，参赛者一组别：" + self.data.group + "，参赛者二姓名：" + self.data.realName2 + "，参赛者二身份证：" + self.data.idNo2 + "，参赛者二组别：" + self.data.group2 + "，联系电话：" + self.data.contact;
    }
    if (self.data.others) {
      remark += "，备注：" + self.data.others;
    }

    wx.showLoading({
      title: '支付中',
    })
    var url = host + '/yq/order/pay';
    var params = {
    }
    if (self.data.productId) {
      params.productId = self.data.productId;
    }
    if (self.data.itemId) {
      params.itemId = self.data.itemId;
    }
    if (self.data.groupId) {
      params.groupId = self.data.groupId;
    }
    var success = function (res) {
      wx.hideLoading()
      var response = res.info
      console.log(response)
      var nonce_str = response.nonce_str
      var prepay_id = response.package
      var sign = response.sign
      var timestamp = response.timeStamp
      var appId = response.appid
      var param = {
        'appId': appId,
        'timeStamp': '' + timestamp,
        'nonceStr': '' + nonce_str,
        'package': 'prepay_id=' + prepay_id,
        'signType': 'MD5',
        'paySign': '' + sign,
        'success': function (res) {
          setTimeout(function () {
            wx.redirectTo({
              url: '/pages/success/success?isFromMore=1'
            })
          }, 200)
        },
        'fail': function (res) {
          wx.showToast({
            title: '支付失败',
            icon: 'none'
          })
        }
      }
      wx.requestPayment(param)
    }
    var fail = function (res) { };
    var complete = function () { };
    app.request(url, params, success, fail, complete);
  }
})